;----------------------
; Quit dialog stuff

quit_keyboard_polling:


	ld		hl,KEY_DOWN
	call	get_keyboard
	and		a
	jr		nz,quit_keyboard_polling_KEY_DOWN
		
	ld		hl,KEY_UP
	call	get_keyboard
	and		a
	jr		nz,quit_keyboard_polling_KEY_UP

	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		nz,quit_keyboard_polling_KEY_SPACE

	;no key was pressed , we should unset keyboard_pressed to 0
	ld		hl,keyboard_pressed
	xor		a
	ld		[hl],a
	
	jr.		quit_keyboard_polling_end


quit_keyboard_polling_KEY_UP:
quit_keyboard_polling_KEY_DOWN:
	call	keyboard_proccess_key
	and		a
	jr.		z,quit_keyboard_polling_end					;key still pressed , exit

	;change yes(0)<->no($FF)
	ld		a,[graphics_buffer+(9*7*2)+1]
	cpl		
	ld		[graphics_buffer+(9*7*2)+1],a
	jr.		quit_keyboard_polling_end

quit_keyboard_polling_KEY_SPACE:
	call	keyboard_proccess_key
	and		a
	jr.		z,quit_keyboard_polling_end					;key still pressed , exit

	;action
	ld		a,[graphics_buffer+(9*7*2)+1]
	and		a
	jr		z,quit_keyboard_polling_QUIT
	;continue game, exit quit menu
	
	pop		af										;setting up the sp register 
	jr.		keyboard_polling_KEY_ESC_after_quit_keyboard_polling	
	

quit_keyboard_polling_QUIT:
	;return to main menu
	xor		a
	ld		[player_tower_height],a
	ld		[player_wall_height],a
	
	/*
	call	hide_cursor_sprites
	
	pop		af 										;mmloop ret address is in the stack
	pop		af										;TODO:do that updating the stack pointer
	pop		af
	pop		af
	jr.		main_menu_entry_point
	*/
	pop		af										;setting up the sp register 
	jr.		keyboard_polling_KEY_ESC_after_quit_keyboard_polling	


quit_keyboard_polling_end:
	

	ret


;--------------------------------------------------------------------------------------------------------------
;Main menu stuff

mmenu_keyboard_polling:
	
	ld		hl,KEY_DOWN
	call	get_keyboard
	and		a
	jr		nz,mmenu_keyboard_polling_KEY_DOWN
		
	ld		hl,KEY_UP
	call	get_keyboard
	and		a
	jr		nz,mmenu_keyboard_polling_KEY_UP

	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		nz,mmenu_keyboard_polling_KEY_SPACE

	;no key was pressed , we should unset keyboard_pressed to 0
	ld		hl,keyboard_pressed
	xor		a
	ld		[hl],a
	
	jr.		mmenu_keyboard_polling_end
mmenu_keyboard_polling_KEY_DOWN:

	call	keyboard_proccess_key
	and		a
	jr.		z,mmenu_keyboard_polling_end					;key still pressed , exit

	;down action
	ld		a,[number_menu_options]
	call	mmenu_update_next_option
	ld		a,[number_menu_options]
	call	mmenu_display_cursor
	
	;ld		a,3
	;call	INICIA_SONIDO
	;ld		b,SFX_CURSOR_UPDOWN
	;call	INICIA_EFECTO
	ld		b,3
	call	INICIA_EFECTO

	
	
	jr		mmenu_keyboard_polling_end

mmenu_keyboard_polling_KEY_UP:

	call	keyboard_proccess_key
	and		a
	jr.		z,mmenu_keyboard_polling_end					;key still pressed , exit

	;up action
	ld		a,[number_menu_options]
	call	mmenu_update_prev_option
	ld		a,[number_menu_options]
	call	mmenu_display_cursor
	
	;ld		b,SFX_CURSOR_UPDOWN
	;call	INICIA_EFECTO
	;ld		a,3
	;call	INICIA_SONIDO
	ld		b,3
	call	INICIA_EFECTO

	jr		mmenu_keyboard_polling_end

mmenu_keyboard_polling_KEY_SPACE:

	call	keyboard_proccess_key
	and		a
	jr.		z,mmenu_keyboard_polling_end					;key still pressed , exit


	;random seed reinit
	ld		a,[JIFFY]
	ld		[seed],a

	;space action
	;ld		b,SFX_ACTION	
	;call	INICIA_EFECTO
	;ld		a,4
	;call	INICIA_SONIDO		
	ld		b,4
	call	INICIA_EFECTO

	
	call	mmenu_perform_action
		
	jr		mmenu_keyboard_polling_end						;TODO: remove if it's needed


mmenu_keyboard_polling_end:
	
	ret


;

;--------------------------------------------------------------------------------------------------------------
;Shop stuff
shop_keyboard_polling:
	ld		hl,KEY_RIGHT
	call	get_keyboard
	and		a
	jr		nz,shop_keyboard_polling_KEY_RIGHT

	ld		hl,KEY_DOWN
	call	get_keyboard
	and		a
	jr		nz,shop_keyboard_polling_KEY_DOWN
		
	ld		hl,KEY_UP
	call	get_keyboard
	and		a
	jr.		nz,shop_keyboard_polling_KEY_UP

	ld		hl,KEY_LEFT
	call	get_keyboard
	and		a
	jr		nz,shop_keyboard_polling_KEY_LEFT
	
	IF	DEMO_MODE==0
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr.		nz,shop_keyboard_polling_KEY_SPACE
	ENDIF
	
	ld		hl,KEY_ESC
	call	get_keyboard
	and		a
	jr.		nz,shop_keyboard_polling_KEY_ESC
	
	;esc


	;no key was pressed , we should unset keyboard_pressed to 0
	ld		hl,keyboard_pressed
	xor		a
	ld		[hl],a
	
	jr.		shop_keyboard_polling_end
shop_keyboard_polling_KEY_RIGHT:

	call	keyboard_proccess_key
	and		a
	jr.		z,shop_keyboard_polling_end					;key still pressed , exit

	;update filter in memory
	
	;;;ld		b,SFX_CURSOR_LEFTRIGHT	
	;call	INICIA_EFECTO
	
	;ld		a,4
	;call	INICIA_SONIDO
	ld		b,4
	call	INICIA_EFECTO

	
	call	shop_move_filter_right
	
	call	shop_filter_leftright_common_code
	;update flags
	ld		a,[dump_flags]
	or		00001111b
	ld		[dump_flags],a

	jr.		shop_keyboard_polling_end

	
	
	
	
	
shop_keyboard_polling_KEY_LEFT:

	call	keyboard_proccess_key
	and		a
	jr.		z,shop_keyboard_polling_end					;key still pressed , exit

	;update filter left

	;ld		b,SFX_CURSOR_LEFTRIGHT	
	;call	INICIA_EFECTO
	;ld		a,4
	;call	INICIA_SONIDO
	ld		b,4
	call	INICIA_EFECTO
	
	
	;update filter in memory
	call	shop_move_filter_left

	call	shop_filter_leftright_common_code
	;update flags
	ld		a,[dump_flags]
	or		00001111b
	ld		[dump_flags],a

	jr.		shop_keyboard_polling_end


shop_keyboard_polling_KEY_DOWN:


	call	keyboard_proccess_key
	and		a
	jr.		z,shop_keyboard_polling_end					;key still pressed , exit


	;update card deck down
	
	;ld		b,SFX_CURSOR_UPDOWN
	;call	INICIA_EFECTO
	;ld		a,3
	;call	INICIA_SONIDO
	ld		b,3
	call	INICIA_EFECTO

	
	ld		a,[shop_filter]
	and		a
	jr		nz,shop_keyboard_polling_KEY_DOWN_withFilter
	ld		b,0
	jr		shop_keyboard_polling_KEY_DOWN_Filter_set
	
shop_keyboard_polling_KEY_DOWN_withFilter:
	ld		b,1
shop_keyboard_polling_KEY_DOWN_Filter_set:
	
	ld		a,[shop_card_list+1]
	call	populate_shop_card_list
	;update	card action
	ld		a,[shop_card_list+3]
	call	print_shop_action
	;update deck
	call	shop_print_deck
	;update	card detail
	ld		a,[shop_card_list+3]
	call	shop_print_card_details_mem
	;update flags
	ld		a,[dump_flags]
	or		00001011b
	ld		[dump_flags],a

	jr.		shop_keyboard_polling_end
	

shop_keyboard_polling_KEY_UP:


	call	keyboard_proccess_key
	and		a
	jr		z,shop_keyboard_polling_end					;key still pressed , exit


	;update card deck up	
	
	;ld		b,SFX_CURSOR_UPDOWN
	;call	INICIA_EFECTO
	;ld		a,3
	;call	INICIA_SONIDO
	ld		b,3
	call	INICIA_EFECTO

	
	ld		a,[shop_filter]
	and		a
	jr		nz,shop_keyboard_polling_KEY_UP_withFilter
	ld		b,0
	jr		shop_keyboard_polling_KEY_UP_Filter_set
	
shop_keyboard_polling_KEY_UP_withFilter:
	ld		b,1
shop_keyboard_polling_KEY_UP_Filter_set:
	push	bc
	ld		a,[shop_card_list]
	call	search_firstcard_backward
	pop		bc											;b filtered option is set
														;a is set
	call	populate_shop_card_list
	;update	card action
	ld		a,[shop_card_list+3]
	call	print_shop_action
	;update deck
	call	shop_print_deck
	;update	card detail
	ld		a,[shop_card_list+3]
	call	shop_print_card_details_mem
	;update flags
	ld		a,[dump_flags]
	or		00001011b
	ld		[dump_flags],a

	jr		shop_keyboard_polling_end


shop_keyboard_polling_KEY_SPACE:


	call	keyboard_proccess_key
	and		a
	jr		z,shop_keyboard_polling_end					;key still pressed , exit

	;perform action
	
	;ld		b,SFX_SHOPACTION	
	;call	INICIA_EFECTO

	
	ld		a,[shop_card_list+3]
	call	shop_action_required
	;returns a=0,1,2 or 3
;		a=1 in
;		a=2 out
;		a=3 bought
	cp		0
	jr		z,shop_keyboard_polling_KEY_SPACE_sound_notaction
	cp		1
	jr		z,shop_keyboard_polling_KEY_SPACE_sound_in
	cp		2
	jr		z,shop_keyboard_polling_KEY_SPACE_sound_out
	;a=3
	ld		b,6
	jr		shop_keyboard_polling_KEY_SPACE_sound_done
shop_keyboard_polling_KEY_SPACE_sound_notaction:
	ld		b,4
	jr		shop_keyboard_polling_KEY_SPACE_sound_done
shop_keyboard_polling_KEY_SPACE_sound_in:
	ld		b,7
	jr		shop_keyboard_polling_KEY_SPACE_sound_done
shop_keyboard_polling_KEY_SPACE_sound_out:	
	ld		b,8
shop_keyboard_polling_KEY_SPACE_sound_done:
	;call	INICIA_SONIDO
	call	INICIA_EFECTO


	;update flags
	ld		a,[dump_flags]
	or		00011000b
	ld		[dump_flags],a
	
	jr		shop_keyboard_polling_end

	
	
shop_keyboard_polling_KEY_ESC:
	call	keyboard_proccess_key
	and		a
	jr.		z,shop_keyboard_polling_end					;key still pressed , exit
	
	
	;hide sprites
	call	hide_shop_sprites
	call	display_shop_fires_fx
	
	;escape action
	;TODO: ask about exiting
	;TODO: try to avoid this pop stuff
	pop		af 												;mmloop ret address is in the stack
	pop		af
	pop		af
	pop		af
	jr.		main_menu_entry_point
	
	
	
	
shop_keyboard_polling_end:

	ret



;end shop stuff
;----------------------------------------------------------------------------------------------------------









keyboard_polling:

	ld		hl,KEY_M
	call	get_keyboard
	and		a
	jr.		nz,keyboard_polling_KEY_M

	ld		a,[turn]
	and		a
	jr		nz,keyboard_polling_clean_pressed_flag

	ld		hl,KEY_RIGHT
	call	get_keyboard
	and		a
	jr		nz,keyboard_polling_KEY_RIGHT

	ld		hl,KEY_DOWN
	call	get_keyboard
	and		a
	jr.		nz,keyboard_polling_KEY_DOWN
		
	ld		hl,KEY_UP
	call	get_keyboard
	and		a
	jr.		nz,keyboard_polling_KEY_UP

	ld		hl,KEY_LEFT
	call	get_keyboard
	and		a
	jr		nz,keyboard_polling_KEY_LEFT

	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr.		nz,keyboard_polling_KEY_SPACE

	ld		hl,KEY_ESC
	call	get_keyboard
	and		a
	jr.		nz,keyboard_polling_KEY_ESC
	

keyboard_polling_clean_pressed_flag:	
	;no key was pressed , we should unset keyboard_pressed to 0
	ld		hl,keyboard_pressed
	xor		a
	ld		[hl],a
	
	jr.		keyboard_polling_end_dont_update_player_card
	

keyboard_polling_KEY_M:
	call	keyboard_proccess_key
	and		a
	jr.		z,keyboard_polling_end					;key still pressed , exit

	ld		a,[music_onoff]
	and		a
	jr		nz,keyboard_polling_KEY_M_MusicOFF
	;off
	ld		a,1
	ld		[music_onoff],a
	ld		a,TUNE_BATTLE
	call	start_music
	jr.		keyboard_polling_end_dont_update_player_card

keyboard_polling_KEY_M_MusicOFF:
	;on
	xor		a
	ld		[music_onoff],a
	call	PLAYER_OFF
	
	
keyboard_polling_KEY_RIGHT:

	call	keyboard_proccess_key
	and		a
	jr.		z,keyboard_polling_end					;key still pressed , exit

	ld		b,12
	call	INICIA_EFECTO


	ld		hl,cursor_data+1
	ld		a,[hl]
	cp		5
	jr		z,keyboard_polling_KEY_RIGHT_TO_0
	inc		a
	ld		[hl],a
	

	
	
	jr.		keyboard_polling_end
keyboard_polling_KEY_RIGHT_TO_0:
	ld		[hl],0
	jr.		keyboard_polling_end
	
keyboard_polling_KEY_LEFT:

	call	keyboard_proccess_key
	and		a
	jr.		z,keyboard_polling_end					;key still pressed , exit


	ld		b,12
	call	INICIA_EFECTO

	ld		hl,cursor_data+1
	ld		a,[hl]
	and		a			;0?
	jr		z,keyboard_polling_KEY_LEFT_TO_5
	dec		a
	ld		[hl],a
	
	
	jr.		keyboard_polling_end
keyboard_polling_KEY_LEFT_TO_5:
	ld		[hl],5
	jr.		keyboard_polling_end

keyboard_polling_KEY_DOWN:


	call	keyboard_proccess_key
	and		a
	jr.		z,keyboard_polling_end					;key still pressed , exit

	ld		b,12
	call	INICIA_EFECTO

	ld		hl,cursor_data
	ld		a,[hl]
	and		a
	jr		z,keyboard_polling_KEY_DOWN_TO_1	
	xor		a
	ld		[hl],a
	
	;ld		a,12
	;call	INICIA_SONIDO


	
	
	jr		keyboard_polling_end
keyboard_polling_KEY_DOWN_TO_1:
	ld		[hl],1	
	jr		keyboard_polling_end
	

keyboard_polling_KEY_UP:


	call	keyboard_proccess_key
	and		a
	jr		z,keyboard_polling_end					;key still pressed , exit


	ld		b,12
	call	INICIA_EFECTO

	ld		hl,cursor_data
	ld		a,[hl]
	dec		a
	jr		z,keyboard_polling_KEY_UP_TO_0	
	ld		[hl],1
	
	;ld		a,12
	;call	INICIA_SONIDO	

	
	
	
	jr		keyboard_polling_end
keyboard_polling_KEY_UP_TO_0:
	ld		[hl],a	
	jr		keyboard_polling_end


keyboard_polling_KEY_SPACE:


	call	keyboard_proccess_key
	and		a
	jr		z,keyboard_polling_end					;key still pressed , exit



	ld		hl,cursor_data
	ld		a,[hl]
	and		a
	jr		nz,keyboard_polling_call_discard
	;check if play is selected when forced_to_drop is set ... it shouldn't be executed
	ld		a,[forced_to_drop]
	and		a
	jr.		nz,keyboard_polling_end							;forced_to_drop!=0 ... let's get out of here! 
															;TODO: SFX here!!!	
	
	call	play_player_selected_card
	;a=0 if card couldn't be played
	and		a
	jr		nz,keyboard_polling_KEY_SPACE_played
	jr		keyboard_polling_end		;player still playing
keyboard_polling_KEY_SPACE_played:	
	ld		a,[play_again]
	and		a							;play again ?;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	jr		nz,keyboard_polling_end		;yes=> don't update turn (still same player)
	ld		a,1
	ld		[turn],a					;cpu turn
	jr		keyboard_polling_end
	
keyboard_polling_call_discard:
	ld		a,1							;check 'don't drop flag'
	call	discard_player_selected_card	
	and		a							;was card discarded?
	jr		nz,keyboard_polling_call_discard_done
	;not allowed SFX
	ld		b,13;******************************************************************************
	call	INICIA_EFECTO

	jr		keyboard_polling_end
keyboard_polling_call_discard_done:		;player still playing	
	;drop SFX 
	ld		b,8
	call	INICIA_EFECTO
	
	ld		a,1
	ld		[turn],a					;cpu turn
	jr		keyboard_polling_end

	
	
keyboard_polling_KEY_ESC:
	call	keyboard_proccess_key
	and		a
	jr.		z,keyboard_polling_end					;key still pressed , exit


	;set quit option to defaul yes (0)
	;graphics_buffer+(9*7)+1 holds this value
	xor		a
	ld		[graphics_buffer+(9*7*2)+1],a
	
	
	call	display_quit_dialog
keyboard_polling_KEY_ESC_quit_keyboard_polling:
	call	quit_keyboard_polling
	call	display_quit_cursor
	jr		keyboard_polling_KEY_ESC_quit_keyboard_polling

keyboard_polling_KEY_ESC_after_quit_keyboard_polling:

	call	display_restore_quit

	jr		keyboard_polling_end
	
keyboard_polling_end:
	;update player card call
	ld		hl,cursor_data+1			;draw card TODO: make sub
	ld		a,[hl]
	ld		b,0
	ld		c,a
	ld		hl,player_hand
	add		hl,bc
	ld		a,[hl]
	ld		l,LCARDART_F_TILE
	call	print_card	
	
	;call	rand
	;jr		keyboard_polling_end_dont_update_player_card


	
keyboard_polling_end_dont_update_player_card:

	ret



;process a key command if key was pressed after no key pressed.
;must be called when a key is pressed
;returns	a=0 if command should be executed, a=1 if may be executed and keyboard_pressed flag is set
keyboard_proccess_key:
	ld		hl,keyboard_pressed
	ld		a,[hl]
	and		a
	jr		z,keyboard_not_previously_pressed
	xor		a
	ret								;ret a=0

keyboard_not_previously_pressed:
	ld		a,1
	ld		[hl],a					;pressed flag set
	ret								;ret a=1







;TODO: I can do something with this and wait_press_space
wait_press_space_flashing_eyes:
	
	ld		hl,tinymage_eyes_struct+1
	dec		[hl]
	jr		nz,wait_press_space_flashing_eyes_skip_eyes_update1
	ld		a,TINY_MAGE_EYES_PERIOD
	ld		[tinymage_eyes_struct+1],a	
	call	update_tinymage_eyes

wait_press_space_flashing_eyes_skip_eyes_update1:
	halt
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		nz,wait_press_space_flashing_eyes
	; nothing pressed now or not space
wait_press_space_flashing_eyes_2:
	
	ld		hl,tinymage_eyes_struct+1
	dec		[hl]
	jr		nz,wait_press_space_flashing_eyes_skip_eyes_update2
	ld		a,TINY_MAGE_EYES_PERIOD
	ld		[tinymage_eyes_struct+1],a	
	call	update_tinymage_eyes

wait_press_space_flashing_eyes_skip_eyes_update2:	
	
	halt

	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		z,wait_press_space_flashing_eyes_2


	call	cls_tinymage_eyes				;remove eyes

	ret
	


	
	
	
wait_press_space:

	;first if is coming with space pressed ... wait until is released
	halt
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		nz,wait_press_space
	; nothing pressed now or not space
wait_press_space_2:
	halt
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		z,wait_press_space_2
	;space pressed
	ret
	
	



;hl keycode passed (from equs)
;ret a =0 no pressed  ,a=1 pressed
get_keyboard:
	ld 		a, l
	push 	hl
	call 	SNSMAT
	pop 	hl
	and 	h
	jr 		z, key_pressed
	xor 	a
	ret
key_pressed:
	ld a, 1
	ret


